#! /bin/sh
# PCP QA Test No. 669
# checks pmmgr large-archive-set functionality
#
# Copyright (c) 2016 Red Hat, Inc.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

which pmmgr >/dev/null 2>&1 || _notrun "No pmmgr binary installed"
echo pmmgr ok

$sudo rm -fr $tmp.dir
$sudo rm -f $tmp.*
rm -f $seq.full

status=1	# failure is the default!
hostname=`hostname`
trap "_cleanup" 0 1 2 3 15

_cleanup()
{
    if [ -n "$pid" ]; then kill $pid; fi
    # restart pmcd and primary pmlogger
    _service pcp restart >>$seq.full 2>&1
    _wait_for_pmcd
    _wait_for_pmlogger
    $sudo rm -fr $tmp.dir
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    tee -a $seq.full |
    sed -e 's,^\[.*\],TIMESTAMP,' \
        -e 's,pmmgr.[0-9]*/[0-9]*.,pmmgr(PID/TID),' \
        -e 's,hostid [a-zA-Z0-9_-.]*,hostid HOSTID,' \
        -e 's,at [a-zA-Z0-9_-.:]*,at LOCAL,' \
        -e 's,'$tmp.dir',TMPDIR,'
}

_filter2()
{
    tee -a $seq.full |
    sed -e 's,'$hostname',HOSTNAME,' \
        -e 's,[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]*[0-9][0-9][0-9][0-9],YYYYMMDD-HHMMSS,'
}

# This test prereqs only pmcd running locally.
# In the future, remote, avahi

echo "hostname=$hostname" >>$seq.full
id >>$seq.full

date >>$seq.full
echo "=== 1. prepare blank pmmgr config directory  ===" | tee -a $seq.full
$sudo rm -rf $tmp.dir
mkdir $tmp.dir
chmod 777 $tmp.dir
ls -ld $tmp.dir >>$seq.full

date >>$seq.full
echo "=== 2. generate a bunch of short archives  ===" | tee -a $seq.full
# put in some blank lines and terminate without \n
echo >>$tmp.dir/target-host
echo >>$tmp.dir/target-host
$PCP_ECHO_PROG $PCP_ECHO_N 'local:'"$PCP_ECHO_C" >>$tmp.dir/target-host
echo $tmp.dir > $tmp.dir/log-directory
echo 'log advisory on default { pmcd.hostname }' > $tmp.dir/pmlogger.conf
echo '-t 1 -s 1 -c '$tmp.dir'/pmlogger.conf' > $tmp.dir/pmlogger  # "one ping only"
# note -v -v here is the same as -D appl0,appl1
env PCP_PMMGR_MERGEBATCH=15 pmmgr -v -v -p 1 -l $tmp.out -c $tmp.dir >$tmp.valout 2>$tmp.valerr &
pid=$!
echo "pid=$pid" >>$seq.full
sleep 5
_service pmcd start >>$seq.full 2>&1
_wait_for_pmcd
sleep 90 # enough for some 
_service pmcd stop >>$seq.full 2>&1

date >>$seq.full
echo "=== 3. check on number of archives and zip 'em up ===" | tee -a $seq.full
ls -l $tmp.dir/$hostname >> $seq.full
num=`ls -1 $tmp.dir/$hostname/*.index | wc -l | sed -e 's/ //g'`
if [ $num -gt 30 ]; then
    echo more than thirty
else
    echo "boo num=$num, expected > 30"
fi
(cd $tmp.dir; tar cf $hostname.tar $hostname)

date >>$seq.full
echo "=== 4. do a few rounds of batched log merging ===" | tee -a $seq.full
# a few blank lines - interpreted as though there was one
echo > $tmp.dir/pmlogmerge
echo VERY IGNORED >> $tmp.dir/pmlogmerge
echo IGNORED >> $tmp.dir/pmlogmerge
_service pmcd start >>$seq.full 2>&1
_wait_for_pmcd
sleep 20 # enough for some cycles of log merging batches
_service pmcd stop >>$seq.full 2>&1

date >>$seq.full
echo "=== 5. check on number of archives after batched logmerge ===" | tee -a $seq.full
ls -l $tmp.dir/$hostname >> $seq.full
num=`ls -1 $tmp.dir/$hostname/*.index | wc -l | sed -e 's/ //g'`
if [ $num -lt 5 ]; then
    echo less than 5
else
    echo "boo num=$num, expected < 5"
fi

date >>$seq.full
echo "=== 5. replace set with original archives  ===" | tee -a $seq.full
(cd $tmp.dir; mv $hostname $hostname.OLD; tar xf $hostname.tar)
ls -l $tmp.dir/$hostname >> $seq.full

date >>$seq.full
echo "=== 6. corrupt ten of them  ===" | tee -a $seq.full
ls -1 $tmp.dir/$hostname/*.meta | head -10 | while read file
do
    dd if=/dev/zero of=$file count=1 bs=512 >/dev/null 2>&1
done

date >>$seq.full
echo "=== 7. do a few rounds of batched log merging ===" | tee -a $seq.full
touch $tmp.dir/pmlogmerge
_service pmcd start >>$seq.full 2>&1
_wait_for_pmcd
sleep 20 # enough for some cycles of log merging batches
_service pmcd stop >>$seq.full 2>&1

date >>$seq.full
echo "=== 8. expect 10 corrupt leftovers  ===" | tee -a $seq.full
ls -1 $tmp.dir/$hostname/corrupt*.meta | wc -l | sed -e 's/ //g'

date >>$seq.full
echo "=== 9. turn on short-term corrupt-gc and cycle a little longer  ===" | tee -a $seq.full
echo 10sec > $tmp.dir/pmlogcheck-corrupt-gc
_service pmcd start >>$seq.full 2>&1
_wait_for_pmcd
sleep 20 # enough for some cycles of log merging batches
_service pmcd stop >>$seq.full 2>&1

date >>$seq.full
echo "=== 10. expect no corrupt leftovers ===" | tee -a $seq.full
ls -1 $tmp.dir/$hostname/corrupt*.meta 2>/dev/null | wc -l | sed -e 's/ //g'

echo "== collecting full pmmgr logs:" >>$seq.full
cat $tmp.out >>$seq.full

echo >>$seq.full
echo "== collecting recent daemon logs:" >>$seq.full
for log in $tmp.dir/$hostname/*.log
do
    if [ -f "$log" ]
    then
	echo "-- $log --" >>$seq.full
	cat $log >>$seq.full
    fi
done

status=0
exit
